﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Module.Core.LoginSystem.Commons.Utilities;

namespace Web.Cms.AjaxCP
{
    public partial class Paging : System.Web.UI.UserControl
    {
        public int totalRecords;
        public int numPerPage;
        public string defaultQueryParam;
        public string queryParam;
        private int numPages = 10;
        public int cPage = 1;
        public bool showReport = true;
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            if (Request.QueryString.Get("cPage") != null)
                cPage = ConvertUtility.ToInt32(Request.QueryString.Get("cPage"));
        }
        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            int totalPages = (int)Math.Ceiling((double)totalRecords / numPerPage);
            int c = (int)Math.Ceiling((double)cPage / numPages);
            int from = (c - 1) * numPages + 1;
            int to = c * numPages;
            if (to > totalPages) to = totalPages;
            ArrayList x = new ArrayList();
            for (int i = from; i <= to; i++) x.Add(i);
            rptPaging.DataSource = x;
            rptPaging.ItemDataBound += new RepeaterItemEventHandler(rptPaging_ItemDataBound);
            rptPaging.DataBind();
            lnkFirst.NavigateUrl = defaultQueryParam;
            lnkLast.NavigateUrl = queryParam + totalPages;
            if (cPage > 1)
                lnkPrev.NavigateUrl = queryParam + (cPage - 1);
            if (cPage < totalPages)
                lnkNext.NavigateUrl = queryParam + (cPage + 1);
            //get some info
            int _from = (cPage - 1) * numPerPage + 1;
            int _to = cPage * numPerPage;
            if (totalRecords == 0)
                lblFrom.Text = "0";
            else
                lblFrom.Text = _from.ToString();
            if (_to > totalRecords)
                lblTo.Text = totalRecords.ToString();
            else
                lblTo.Text = _to.ToString();
            lblTotal.Text = totalRecords.ToString();
            if (totalRecords == 0) this.Visible = false;
            pnReport.Visible = showReport;
        }

        public void rptPaging_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            HyperLink lnkPageBreak = (HyperLink)e.Item.FindControl("lnkPageBreak");
            lnkPageBreak.NavigateUrl = queryParam + e.Item.DataItem;
            lnkPageBreak.Text = ConvertUtility.ToString(e.Item.DataItem);
            if (cPage == ConvertUtility.ToInt32(lnkPageBreak.Text))
            {
                lnkPageBreak.ForeColor = Color.White;
                lnkPageBreak.BackColor = Color.FromName("#377de4");
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {

        }
    }
}